package org.one.stone.soup.grfxML.plugin.web;

import java.io.IOException;
import java.io.InputStream;

import org.one.stone.soup.grfxML.DataState;
import org.one.stone.soup.grfxML.DataString;
import org.one.stone.soup.grfxML.GrfxMLEngine;
import org.one.stone.soup.grfxML.TagModifier;
import org.one.stone.soup.grfxML.plugin.SimplePlugin;
import org.one.stone.soup.grfxML.plugin.grfxMLCaster;
import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;

public class FilePoster extends SimplePlugin {

	public static final int ARG_TRIGGER = 0;
	public static final int ARG_URL = 1;
	public static final int ARG_FILE = 2;

	DataState trigger = new DataState();
	DataString url = new DataString(TagModifier.STRING_TYPE_FILE);
	DataString file = new DataString();

/**
 * StateInverter constructor comment.
 */
public FilePoster(GrfxMLEngine engine){
	super(engine);
}
/**
 * initialize method comment.
 */
public void initialize()
{
}
/**
 * process method comment.
 */
public void process()
{
 try{
	if(trigger.getValue()==true)
	{
		trigger.setValue( false,this );

		try{
		 String fileName = file.getValue();

		 InputStream iStream = org.one.stone.soup.browser.Browser.postFile( url.getValue(),fileName );

		 getEngine().getAPI().load(iStream,"Response from post of "+fileName+" to "+url.getValue());

		 iStream.close();
		}
		catch(IOException ie)
		{
			ie.printStackTrace();
		}
	}
 }catch(LockException le){}
}
/**
 * register method comment.
 */
public void register(DataLinker store)
{

	trigger = grfxMLCaster.cast(trigger,getArg(trigger,ARG_TRIGGER,store));
	file = grfxMLCaster.cast(file,getArg(file,ARG_FILE,store));
	url = grfxMLCaster.cast(url,getArg(file,ARG_URL,store));

}
/**
 * replace method comment.
 */
public void replace(Field oldObj, Field newObj)
{

	trigger = grfxMLCaster.replace(trigger,oldObj,newObj);
	file = grfxMLCaster.replace(file,oldObj,newObj);
	url = grfxMLCaster.replace(url,oldObj,newObj);

}
/**
 * stop method comment.
 */
public void stop() {}
}
